﻿using Castle.Core.Configuration;
using Inovout.Runtime;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Inovout.Web.Mvc
{
    public static class ContentExtensions
    {
        private const string defaultThemeRootPath = "Styles/";
        private static readonly string defaultThemeName;

        static ContentExtensions()
        {
            IApplication application = ComponentRegistry.Resolve<IApplication>();
            IConfiguration settings = application.Configuration.Children.SingleOrDefault(c => c.Name.Equals("settings"));
            if (settings == null)
            {
                defaultThemeName = "Classic";

            }
            else
            {
                IConfiguration defaultThemeSetting = settings.Children.SingleOrDefault(c => c.Attributes.AllKeys.Contains("name") && c.Attributes["name"].Equals("DefaultTheme"));
                defaultThemeName = defaultThemeSetting == null ? "Default" : defaultThemeSetting.Attributes["value"];
            }

        }
        public static IHtmlString Script(this HtmlHelper html, string scriptPath)
        {
            ContentResource scriptResource = ContentResource.ScriptResources.SingleOrDefault(s => s.Uri.Equals(scriptPath));
            if (scriptResource == null)
            {
                TagBuilder tagBuilder = new TagBuilder("script");
                tagBuilder.Attributes.Add("src", UrlHelper.GenerateContentUrl(scriptPath, html.ViewContext.HttpContext));
                //tagBuilder.MergeAttribute(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes))
                return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else if (!string.IsNullOrEmpty(scriptResource.CdnUri))
            {
                TagBuilder tagBuilder = new TagBuilder("script");
                tagBuilder.Attributes.Add("src", UrlHelper.GenerateContentUrl(scriptResource.CdnUri, html.ViewContext.HttpContext));

                return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else
            {
                return Scripts.Render(scriptPath);
            }
        }

        internal static IHtmlString StyleInternal(this HtmlHelper html, string stylePath)
        {
            ContentResource styleResource = ContentResource.StyleResources.SingleOrDefault(style => style.Uri.Equals(stylePath));
            if (styleResource == null)
            {
                TagBuilder styleTagBuilder = new TagBuilder("link");
                styleTagBuilder.Attributes.Add("href", UrlHelper.GenerateContentUrl(stylePath, html.ViewContext.HttpContext));
                styleTagBuilder.Attributes.Add("rel", "style");
                styleTagBuilder.Attributes.Add("type", "text/css");

                return MvcHtmlString.Create(styleTagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else if (styleResource.HasCdnUri)
            {
                TagBuilder styleTagBuilder = new TagBuilder("link");
                styleTagBuilder.Attributes.Add("href", UrlHelper.GenerateContentUrl(styleResource.CdnUri, html.ViewContext.HttpContext));
                styleTagBuilder.Attributes.Add("rel", "style");
                styleTagBuilder.Attributes.Add("type", "text/css");

                return new MvcHtmlString(styleTagBuilder.ToString(TagRenderMode.SelfClosing));
            }
            else
            {
                return Styles.Render(stylePath);
            }
        }
        public static IHtmlString Style(this HtmlHelper html, string stylePath)
        {
            return MvcHtmlString.Create(html.StyleInternal(stylePath).ToString() + html.Skin(stylePath));
        }
        public static IHtmlString Image(this HtmlHelper html, string imageName, object htmlAttributes = null)
        {
            TagBuilder tagBuilder = new TagBuilder("img");
            string imagePath = BuildThemePath(string.Format("~/{0}Images/{1}", defaultThemeRootPath, imageName));
            tagBuilder.Attributes.Add("src",
                 UrlHelper.GenerateContentUrl(imagePath, html.ViewContext.HttpContext));
            tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }
        public static IHtmlString Skin(this HtmlHelper html, string themePath)
        {
            return StyleInternal(html, BuildThemePath(themePath));
        }
        private static string BuildThemePath(string path)
        {

            string themeName = defaultThemeName;
            return path.Insert(path.IndexOf(defaultThemeRootPath) + defaultThemeRootPath.Length, "Themes/" + themeName + "/");

        }
    }
}